package Matrix.Medium;

public class LC0048 {
    public static void rotate(int[][] matrix) {
        int N = matrix.length;
        double centerX = (N - 1) * 1.0 / 2, centerY = (N - 1) * 1.0 / 2; // 这里用x表示行，与数组的表示方式保持一致
        for (int i = 0; i <= (N % 2 == 0 ? (N - 1) / 2 : (N - 1) / 2 - 1); i++) {
            for (int j = 0; j <= (N - 1) / 2; j++) {
                int topLeft = matrix[i][j];
                double deltaX = i - centerX, deltaY = j - centerY;
                int topRight = matrix[(int) (deltaY + centerX)][(int) (-deltaX + centerY)];
                int bottomRight = matrix[(int) (-deltaX + centerX)][(int) (-deltaY + centerY)];
                int bottomLeft = matrix[(int) (-deltaY + centerX)][(int) (deltaX + centerY)];
                // 轮换
                matrix[(int) (deltaY + centerX)][(int) (-deltaX + centerY)] = topLeft;
                matrix[(int) (-deltaX + centerX)][(int) (-deltaY + centerY)] = topRight;
                matrix[(int) (-deltaY + centerX)][(int) (deltaX + centerY)] = bottomRight;
                matrix[i][j] = bottomLeft;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        rotate(matrix);
    }
}
